Trace Pascal
Volume Number: 5
Issue Number: 1
Column Tag: Programmer's Workshop
Trace LightSpeed Pascal 
By Alan Wootton, Santa Monica, CA, MacTutor Contributing Editor
Note: Source code files accompanying article are located on MacTech CD-ROM orsource code disks.
Alan Wootton currently of MTS Informix Software, Los Angeles, is a famous early
contributor of MacTutor and is well known in Mac programming circles. We welcome
his return with this article.
A method of obtaining an indented listing recording the execution history, and call
chain, of any LightSpeed Pascal program, is presented.
Once upon a time (in 1975) in a far away land (Arizona) there was a young man
(your humble author) who went to a great place of learning (ASU) to seek his fame and
fortune. While he was there he happened upon a course of study that involved the
learning of a strange new numerical language (Fortran) that could be read and
understood by a new breed of Machine Folk known as ‘Computers’. Unfortunately for
the students in this class, it was necessary to learn to read and understand as Machine
Folk, and the ways of the Machine Folk were strange indeed. This was a most tedious
and difficult job. Surely only Wizards could train their brains to such a task!
Often was the day when these poor disciples would be presented with a riddle
(translated here into a more moderne language for all ye moderne folk) such as:
PROBLEM SET #4 (25 points) Given the procedure:
PROCEDURE A (i: integer);
begin
if i > 2 then
begin
write(i);
A(i div 2);
A(i - i div 2);
end;
end;
4a) Please write the the output produced by the call:
A(123);
Whereupon the diligent student would, after much mental effort, correctly write:
123 61 30 15 7 3 4 8 4 4 15 7 3 4 8 4 4 31 15 7 3 4 8 4 4 16 8 4 4 8
4 4 62 31 15 7 3 4 8 4 4 16 8 4 4 8 4 4 31 15 7 3 4 8 4 4 16 8 4 4 8
4 4
The result of such a wizardly task was the bestowal of the desirable, yet
non-negotiable, commendation known as the ‘A’. Provided of course that the student
correctly answered the other parts:
4b) How many times is the procedure A invoked?
4c) What is the maximum depth to which A recurses?
The answers being, of course:
{See if you can guess. Answers at the end of the article}
As you might imagine, a great call was heard throughout the land (or at least the
classroom). “Why must we learn to do the work of the machine folk when it is so
admirably done by those very folk”. The answer was “so that ye may all be able to
recognize the errors of the machine folk”. Errors of the machine folk? Yes, the
machine folk, being of simple nature, were capable of doing only as told. When those
directions were the least bit imprecise then resulting machine actions could defy all
gentle reason.
At that time I thought “why don’t we teach the machines to find their own
mistakes?” Being a neophyte, and not knowing the nature of the machine instructions
required for such a task, I did not pursue this topic any further.
Now, things are different.
A Goal
Often I am faced with the daunting task of understanding the execution of a large,
complicated, and sometimes foreign, pieces of code. My tools are the writeln, the
breakpoint, the observe window, and the rest of the fine features of the LightSpeed
development environment. Additionally I usually code routines to monitor the
correctness of difficult data structures. And recently, I have developed a method of
using the ‘hooks’ that LightSpeed puts in its code to monitor the entire execution of a
program.
Having the ability to monitor the execution of a program brings up several
possibilities. One could stop at the end of every procedure and verify that all the data
structures are intact, or that the heap is not damaged, or that an excessive amount of
time has not elapsed. As space is limited in this article I shall present what I think is a
most interesting utility: A method of producing an indented listing of all procedures
called.
Given the procedure above we wish to obtain a listing such that the call: A(7)
produces the result:
BEGIN A
. BEGIN A
. . BEGIN A
. . END A
. . BEGIN A
. . END A
. END A
. BEGIN A
. . BEGIN A
. . END A
. . BEGIN A
. . END A
. END A
END A